package cn.xfdzx.android.apps.shop.activity.remake;


import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.adapter.remake.OrderMyRemake_MarketAdapter;
import cn.xfdzx.android.apps.shop.adapter.remake.OrderMyRemake_RetailAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.DissatisfiedReasonBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.bean.PeiSongBeen;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListMarketBean;
import cn.xfdzx.android.apps.shop.bean.ShopRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.contract.OrderRemakeContract;
import cn.xfdzx.android.apps.shop.presenter.OrderRemakePresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;

/*个人中心我的评价页面*/
public class MyRemakeActivity extends BaseMvpActivity<OrderRemakePresenter>implements OrderRemakeContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;      //标题栏-左边按钮
    @BindView(R.id.recycle_remake)
    RecyclerView recycle_remake;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private List<MyRemakeListRetailBean.DataBean> retailDate = new ArrayList<>();//零售我的评价列表
    private OrderMyRemake_RetailAdapter retailAdapter;//零售我的评价适配器

    private List<List<MyRemakeListMarketBean.DataBean>> marketDate;//批发我的评价列表
    private OrderMyRemake_MarketAdapter marketAdapter;//批发我的评价适配器

    private int page = 1;
    private int limit = Constants.LIMIT;
    private List<MyRemakeListMarketBean.DataBean> dataBeans = new ArrayList<>();//拆分合并后的店铺数组
    private View emptyView;
    @Override
    public int getLayoutId() {
        return R.layout.activity_myremake;
    }

    @Override
    public void initView() {
        mPresenter = new OrderRemakePresenter();
        mPresenter.attachView(this);
        if (MainActivity.TYPE_IS_RETAIL == 0){
            mPresenter.getMyRemakeListRetail(true,true,limit+"" ,page);
        }else {
            mPresenter.getMyRemakeListMarket(true,true,limit+"" ,page);
        }
        initRecycle();
    }

    private void initRecycle() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (MainActivity.TYPE_IS_RETAIL == 0){
                    mPresenter.getMyRemakeListRetail(true,false,limit+"" ,page);
                }else {
                    mPresenter.getMyRemakeListMarket(true,false,limit+"" ,page);
                }

            }
        });

        if (MainActivity.TYPE_IS_RETAIL == 0){
            retailAdapter = new OrderMyRemake_RetailAdapter(retailDate);
            recycle_remake.setLayoutManager(new LinearLayoutManager(this));
            recycle_remake.setAdapter(retailAdapter);
            //删除回调
            retailAdapter.setItemDeleteClickListener(new OrderMyRemake_RetailAdapter.OnItemOnClickListener() {
                @Override
                public void onItemOnClick(int position, MyRemakeListRetailBean.DataBean date) {
                     mPresenter.getDeleteRemake(date.getReplay().getId()+"",MainActivity.TYPE_IS_RETAIL+1);
                }
            });

            emptyView = getLayoutInflater().inflate(R.layout.view_emptyview, null);
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //marketAdapter.setHeaderAndEmpty(true);
            retailAdapter.setEmptyView(emptyView);
        }else {
            marketAdapter = new OrderMyRemake_MarketAdapter(dataBeans,mPresenter);
            recycle_remake.setLayoutManager(new LinearLayoutManager(this));
            recycle_remake.setAdapter(marketAdapter);

            emptyView = getLayoutInflater().inflate(R.layout.view_emptyview, null);
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //marketAdapter.setHeaderAndEmpty(true);
            marketAdapter.setEmptyView(emptyView);
        }



    }


    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onUploadMoreImgSuccess(AfterServiceUpLoadMoreImgBean bean) {

    }

    @Override
    public void onUploadSingImgSuccess(AfterServiceUpLoadSingImgBean bean) {

    }

    @Override
    public void onGetDissatisfiedSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void onGetRemakeRetailSuccess(DissatisfiedReasonBean bean) {

    }

    @Override
    public void getPeisongInfoSuccess(PeiSongBeen bean) {

    }

    @Override
    public void onGetRemakeMarketSuccess(DissatisfiedReasonBean bean) {

    }

    /**我的评价列表-零售*/
    @Override
    public void onGetMyRemakeListRetailSuccess(MyRemakeListRetailBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {

            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.getMyRemakeListRetail(false,true,limit+"",page);
                    }
                });
            }

            if (page == 1){
                mRefreshLayout.setNoMoreData(false);
                retailDate.clear();
                if (EmptyUtil.isNotEmpty(bean.getData())){
                    retailDate = bean.getData();
                    retailAdapter.refreshDatas(retailDate);
                }else {
                    retailAdapter.clearDatas();
                }
            }else {
                if (EmptyUtil.isNotEmpty(bean.getData())){
                    //retailDate.addAll(bean.getData());
                    retailAdapter.addData(bean.getData());
                   // retailAdapter.notifyDataSetChanged();
                }
            }

            if (EmptyUtil.isEmpty(bean.getData())) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }

        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onGetMyRemakeListMarketSuccess(MyRemakeListMarketBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {

            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.getMyRemakeListMarket(false,true,limit+"",page);
                    }
                });
            }

            if (page == 1){
                mRefreshLayout.setNoMoreData(false);
                dataBeans.clear();
                if (EmptyUtil.isNotEmpty(bean.getData())){
                    for (int i = 0; i < bean.getData().size(); i++) {
                        dataBeans.addAll(bean.getData().get(i));
                    }
                    marketAdapter.refreshDatas(dataBeans);
                }else {
                    marketAdapter.clearDatas();
                }

            }else {
                if (EmptyUtil.isNotEmpty(bean.getData())){
                    for (int i = 0; i < bean.getData().size(); i++) {
                        //dataBeans.addAll(bean.getData().get(i));
                        marketAdapter.addData(bean.getData().get(i));
                    }
                }
             //   marketAdapter.notifyDataSetChanged();
            }

            if (EmptyUtil.isEmpty(bean.getData())) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }


        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onGetDeleteRemakeSuccess(MyRemakeListMarketBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            page = 1;
            if (MainActivity.TYPE_IS_RETAIL == 0){
                mPresenter.getMyRemakeListRetail(true,true,limit+"",page);
            }else{
                mPresenter.getMyRemakeListMarket(true,true,limit+"",page);
            }
        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void onGetShopRemakeRetailSuccess(ShopRemakeListRetailBean bean) {

    }

    @Override
    public void onGetShopRemakeMarketSuccess(ShopRemakeListMarketBean bean) {

    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;

        }
    }
}
